package org.csu.molediary.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.molediary.api.common.CommonResponse;
import org.csu.molediary.api.entity.Account;
import org.csu.molediary.api.entity.Friend;
import org.csu.molediary.api.persistence.AccountMapper;
import org.csu.molediary.api.persistence.FriendMapper;
import org.csu.molediary.api.service.FriendService;
import org.csu.molediary.api.vo.FriendVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service("FriendService")
public class FriendServiceImpl implements FriendService {

    @Autowired
    private FriendMapper friendMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Override
    //获取某用户所有好友
    public CommonResponse<List<FriendVO>> getAllFriendByMe(String me) {
        QueryWrapper<Friend> friendQueryWrapper = new QueryWrapper<>();
        friendQueryWrapper.eq("me",me);
        List<FriendVO> friendVOList = new ArrayList<>();

        List<Friend> friendList = friendMapper.selectList(friendQueryWrapper);

        //为friendVOList插入Friend对象
        Iterator<Friend> friendIterator = friendList.iterator();
        while(friendIterator.hasNext()){
            Friend friend = friendIterator.next();
            FriendVO friendVO = new FriendVO();
            friendVO.setFriend(friend);

            //通过id查找account
            Account account = accountMapper.selectById(me);
            Account account1 = accountMapper.selectById(friend.getMefriend());

            friendVO.setMefriendname(account1.getName());
            friendVO.setMename(account.getName());
            friendVO.setHead(account1.getHead());

            friendVOList.add(friendVO);
        }
        if(friendVOList.isEmpty()){
            return CommonResponse.createForSuccessMessage("某用户没有好友");
        }
        return CommonResponse.createForSuccess("获取某用户所有好友成功",friendVOList);
    }

    @Override
    //添加好友,这里每添加一个好友要添加两条
    public CommonResponse<FriendVO> addFriendById(String me, String mefriend) {
        QueryWrapper<Friend> friendQueryWrapper = new QueryWrapper<>();
        friendQueryWrapper.eq("me",me).eq("mefriend",mefriend);
        List<Friend> friendList = friendMapper.selectList(friendQueryWrapper);
        //如果好友表中有两个人是好友的信息，则不能再次添加为好友
        if(!friendList.isEmpty()){
            return CommonResponse.createForSuccessMessage("两个人已经是好友了");
        }
        friendQueryWrapper.clear();

        //向好友表中插入两条
        Friend friend = new Friend();
        Friend friend1 = new Friend();
        friend.setMe(me);
        friend.setMefriend(mefriend);
        friend1.setMe(mefriend);
        friend1.setMefriend(me);

        friendMapper.insert(friend);
        friendMapper.insert(friend1);

        //返回friendVO类
        FriendVO friendVO = new FriendVO();
        friendVO.setFriend(friend);

        //通过id查找account
        Account account = accountMapper.selectById(me);
        Account account1 = accountMapper.selectById(mefriend);

        friendVO.setMefriendname(account1.getName());
        friendVO.setMename(account.getName());
        friendVO.setHead(account1.getHead());


        return CommonResponse.createForSuccess("添加好友成功",friendVO);
    }

    @Override
    //通过name模糊查询好友
    public CommonResponse<List<Account>> getFriendByNameLike(String me,String namelike) {
        QueryWrapper<Friend> friendQueryWrapper = new QueryWrapper<>();
        friendQueryWrapper.eq("me",me);

        List<Friend> friendList = friendMapper.selectList(friendQueryWrapper);
        List<Account> accountList = new ArrayList<>();

        //为friendVOList插入Friend对象
        Iterator<Friend> friendIterator = friendList.iterator();
        while(friendIterator.hasNext()){
            Friend friend = friendIterator.next();
            QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
            accountQueryWrapper.eq("id",friend.getMefriend());
            accountQueryWrapper.like("name",namelike);
            Account account = accountMapper.selectOne(accountQueryWrapper);

            //如果这个好友的昵称符合模糊查询的要求
            if(account!=null){
                accountList.add(account);
            }
        }
        if(accountList.isEmpty()){
            return CommonResponse.createForError("没有查询到昵称中含有该字段的好友");
        }
        return CommonResponse.createForSuccess("查询好友成功",accountList);
    }
}
